goog.provide('caffeine.Model.validators.FromFunction');
goog.provide('caffeine.Model.validators.ValidationError');

goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('goog.format.EmailAddress');

/**
 * @constructor
 */
caffeine.Model.validators.ValidationError = function(input, model, opt_message) {

};

/**
 * Generic validator that takes a function. Expects function true or
 *  *return* an error message
 *
 * @constructor
 * @param {Function} testFunction Function to use to validate.
 * @param {Object=} opt_context Context to call test from.
 */
caffeine.Model.validators.Fn = function(testFunction, opt_context) {
  if (!goog.isFunction(testFunction)) throw Error('Not a valid function!');

  /**
   * @type {Function}
   * @private
   */
  this.testFn_ = testFunction;

  /**
   * @type {Object}
   * @private
   */
  this.context_ = opt_context || this;
};

/**
 * Method that gets called on model inputs
 * @param {*} input The input to validate.
 * @param {caffeine.Model} model The model field is being validated for.
 */
caffeine.Model.validators.Fn.prototype.validate = function(input, model) {
  var testResult = this.testFn_.call(this.context_, input, model);
  if (goog.isBoolean(testResult) && testResult) {
    return true;
  } else if (goog.isBoolean(testResult)) {
    var errorMessage = 'An error occured!';
  }

  // Throw validation error
  var error = new caffeine.Model.validators.ValidationError(
    input, model, errorMessage || testResult);
  model.dispatchEvent(error);

  // Reply and say, "I'm Invalid!"
  return false;
};
